package pers.wensheng.patterns.behavioral_pattern.iterator_pattern.jdk_impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class MyContainer implements Iterable<Student>{

    private List<Student> list = new ArrayList<>();

    public void add(Student student){
        list.add(student);
    }
    public void remove(Student student){
        list.remove(student);
    }

    /**
     * Returns an iterator over elements of type {@code T}.
     *
     * @return an Iterator.
     */
    @Override
    public Iterator<Student> iterator() {
        return new Itr(this.list);
    }
    private class Itr implements Iterator{

        private List<Student> list;

        private int point;

        public Itr(List<Student> list) {
            this.list = list;
        }

        @Override
        public boolean hasNext() {
            return this.list.size()>point;
        }

        @Override
        public Object next() {
            Student res = this.list.get(point);
            point++;
            return res;
        }
    }

}
